package cn.yunyichina.test.reservation.pay;

import org.junit.Test;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Lullaby on 2016/9/2.
 */
public class MockWechatPayNotify {

    @Test
    public void mockWechatPayNotify() throws IOException {
        System.out.println("begin send");
        String inputParam = "<xml>\n" +
                "  <appid><![CDATA[wx2421b1c4370ec43b]]></appid>\n" +
                "  <attach><![CDATA[支付测试]]></attach>\n" +
                "  <bank_type><![CDATA[CFT]]></bank_type>\n" +
                "  <fee_type><![CDATA[CNY]]></fee_type>\n" +
                "  <is_subscribe><![CDATA[Y]]></is_subscribe>\n" +
                "  <mch_id><![CDATA[10000100]]></mch_id>\n" +
                "  <nonce_str><![CDATA[5d2b6c2a8db53831f7eda20af46e531c]]></nonce_str>\n" +
                "  <openid><![CDATA[oUpF8uMEb4qRXf22hE3X68TekukE]]></openid>\n" +
                "  <out_trade_no><![CDATA[Y20160901174845017]]></out_trade_no>\n" +
                "  <result_code><![CDATA[SUCCESS]]></result_code>\n" +
                "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                "  <sign><![CDATA[B552ED6B279343CB493C5DD0D78AB241]]></sign>\n" +
                "  <sub_mch_id><![CDATA[10000100]]></sub_mch_id>\n" +
                "  <time_end><![CDATA[20140903131540]]></time_end>\n" +
                "  <total_fee>1</total_fee>\n" +
                "  <trade_type><![CDATA[JSAPI]]></trade_type>\n" +
                "  <transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id>\n" +
                "</xml>";
        URL url = new URL("http://127.0.0.1:8080/reservation/pay/notify/wechat");
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        HttpURLConnection.setFollowRedirects(true);
        httpConn.setDoOutput(true);
        httpConn.setRequestMethod("POST");
        httpConn.setRequestProperty("Content-Type", "text/xml");
        httpConn.connect();
        OutputStream output = httpConn.getOutputStream();
        try (OutputStreamWriter outr = new OutputStreamWriter(output, "UTF-8")) {
            outr.write(inputParam.toString().toCharArray(), 0, inputParam.toString().length());
            outr.flush();
            outr.close();
        }
        System.out.println("send ok");
        int code = httpConn.getResponseCode();
        System.out.println("code " + code);
        System.out.println(httpConn.getResponseMessage());

        String sCurrentLine;
        StringBuffer sTotalString = new StringBuffer();
        if (code == 200) {
            java.io.InputStream is = httpConn.getInputStream();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
                while ((sCurrentLine = reader.readLine()) != null)
                    if (sCurrentLine.length() > 0)
                        sTotalString.append(sTotalString).append(sCurrentLine.trim());
            }
        } else {
            sTotalString.append("Remote server error, code -> ").append(code);
        }
        System.out.println("response -> " + sTotalString);
    }

}
